export function getClsName(obj: object) {
    return obj.constructor.name;
}

export function copyObj<T>(obj: T): T {
    if (!obj) {
        return obj;
    }
    if (typeof obj === "object") {
        if (obj instanceof Array) {
            return obj.map((item) => {
                return copyObj(item);
            }) as any as T;
        }
        const copiedObj: any = {};
        for (const attr in obj) {
            if (typeof obj[attr] === "object") {
                copiedObj[attr] = copyObj(obj[attr]);
            } else {
                copiedObj[attr] = obj[attr];
            }
        }
        return copiedObj;
    }
    return obj;
}

export function jsonCopy<T>(obj: object): T {
    const text = JSON.stringify(obj);
    return JSON.parse(text) as any as T;
}

export function mixedObj<T, U>(objA: T, objB: U, whenNullUse: "a" | "b" = "b"): T & U {
    const copyA = copyObj(objA) as any;
    const copyB = copyObj(objB) as any;
    for (const attr in copyB) {
        if (copyB[attr] != null || whenNullUse !== "a") {
            copyA[attr] = copyB[attr];
        }
    }
    return copyA as T & U;
}
